package com.fivefu.core.camera.services.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.core.camera.entity.DbMonitorLabel;
import com.fivefu.core.camera.mapper.DbMonitorLabelMapper;
import com.fivefu.core.camera.services.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-20 19:45:26
 */
@Service
public class DbMonitorLabelServiceImpl extends ServiceImpl<DbMonitorLabelMapper, DbMonitorLabel> implements DbMonitorLabelService {



    /**
     * 通过标签名称模糊查询标签编号
     * @param labelName 标签名称
     * @return
     */
    @Override
    public List<Long> queryLabelIdsByLabelName(String labelName){
        QueryWrapper<DbMonitorLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.like("label_name", labelName);
        wrapper.select("id");
        List<DbMonitorLabel> labelList = baseMapper.selectList(wrapper);
        List<Long> labels = labelList.stream().map(k -> k.getId()).collect(Collectors.toList());
        return labels;
    }

    /**
     * 通过标签名称添加标签
     * @param labelId 标签编号
     * @param labelName 标签名称
     * @param userId 用户编号
     */
    @Override
    public Long editLabelByLabelName(Long labelId, String labelName, String userId) {
        LocalDateTime time = LocalDateTime.now();
        DbMonitorLabel label = new DbMonitorLabel();
        label.setLabelName(labelName);
        label.setUpdatedBy(userId);
        label.setUpdatedTime(time);
        if (labelId != null){
            label.setId(labelId);
            baseMapper.updateById(label);
        }else{
            label.setIsDelete(0);
            label.setCreatedBy(userId);
            label.setCreatedTime(time);
            baseMapper.insert(label);
        }
        return label.getId();
    }

    /**
     * 通过标签名称模糊查询标签名称
     * @param labelName 标签名称
     * @return
     */
    @Override
    public DbMonitorLabel queryMonitorLabelByLabelName(String labelName) {
        QueryWrapper<DbMonitorLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("label_name",labelName);
        wrapper.eq("is_delete",0);
        return baseMapper.selectOne(wrapper);
    }

    /**
     *返回标签信息列表
     */
    @Override
    public Page<DbMonitorLabel> queryLabelList(Integer page,Integer limit) {
        Page<DbMonitorLabel> returnPage = new Page<>();

        List<DbMonitorLabel> list = new ArrayList<>();

        QueryWrapper<DbMonitorLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);

        //判断是否分页
        if (page != null && limit != null){
            IPage iPage = new Page<>(page, limit);
            iPage = page(iPage, wrapper);
            returnPage.setTotal(iPage.getTotal());
            list = iPage.getRecords();
        }else {
            list = list(wrapper);
            returnPage.setTotal(list.size());
        }

        returnPage.setRecords(list);
        return returnPage;
    }

    /**
     * 查询标签
     * @param labelId 标签id
     * @return
     */
    @Override
    public DbMonitorLabel queryLabelById(Long labelId) {
        QueryWrapper<DbMonitorLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("id",labelId);
        return getOne(wrapper);
    }
}
